import '@js/init.js'
import { subjectData, classRender,changesStudent,changeStudentData } from '../utils/getData'
//动态渲染
show();

async function show() {
    const data =location.search.split('=')[1];
    const rem = await changeStudentData(data);
    //渲染专业动态框
    await  objectlistRender()
    async function objectlistRender() {
        const data = await subjectData();
        const texthtml = data.map(item => {
            return ` <option value="${item._id}">${item.name}</option>`
        });
        $('#updateSubjectsId').html(texthtml);
        
    }
    $('#studentsName').val(rem.name);
    $('#studentsAge').val(rem.age);
    $(`[name=studentsGender][value=${rem.sex}]`).prop('checked', true);
    $('#updateSubjectsId').val(rem.objectId);
    let idd = $('#updateSubjectsId').val();
     console.log(idd);
    const text = await classRender(idd);
    // console.log(text);
    text.forEach(elem => {
        $('#updateClassesId').append(`<option value="${elem._id}">${elem.name}</option>`);
    });
    $('#updateClassesId').val(rem.clasId);


}

$('#updateSubjectsId').change(async function () {
    console.log('hhha');

    let id = $('#updateSubjectsId').val();
    const text = await classRender(id);
    $('#updateClassesId').empty();
    text.forEach(elem => {
        $('#updateClassesId').append(`<option value="${elem._id}">${elem.name}</option>`);
    });
})



//确认修改
$('.blue').click( async function () {
    let name = $('#studentsName').val();
    let age = $('#studentsAge').val();
    let sex = $("[name=studentsGender]:checked").val();
    let object = $('#updateSubjectsId').val();
    let clas = $('#updateClassesId').val();
    // console.log(object, clas);

    const rem = await changesStudent(id, name, age, sex, object, clas);
    alert(rem);
})